/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.dao.TbMpWalletMapper;
import com.thinkgem.jeesite.modules.sys.dao.TbWithdrawApplyMapper;
import com.thinkgem.jeesite.modules.sys.dao.WithDrawApplyDao;
import com.thinkgem.jeesite.modules.sys.entity.TbMpWallet;
import com.thinkgem.jeesite.modules.sys.entity.TbWithdrawApply;
import com.thinkgem.jeesite.modules.sys.utils.NumberGenerator;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 提现Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class WithDrawApplyService extends CrudService<WithDrawApplyDao, TbWithdrawApply> {
	@Autowired
	private TbWithdrawApplyMapper tbWithdrawApplyMapper;
    @Autowired
	private TbMpWalletMapper tbMpWalletMapper;


    @Transactional(readOnly = false)
	public TbWithdrawApply insert(TbWithdrawApply tbWithdrawApply) {
        //麻雀钱包冻结金额
        // TODO: 11/26/2016 总额扣掉冻结金额  冻结金额累加
        TbMpWallet tbMpWallet= tbMpWalletMapper.getByUID(UserUtils.getUser().getId());
        tbMpWallet.setTotalMoney(tbMpWallet.getTotalMoney().subtract(tbWithdrawApply.getMoney()));
        tbMpWallet.setFrozenMoney(tbMpWallet.getFrozenMoney().add(tbWithdrawApply.getMoney()));
        tbMpWalletMapper.updateMqWallet(tbMpWallet);

        //c插入提现记录
		tbWithdrawApply.setAuditorStatus("0");
		tbWithdrawApply.setIsDelete((byte)0);
		tbWithdrawApply.setStreamNo(NumberGenerator.getNextStreamNo());
		tbWithdrawApplyMapper.insert(tbWithdrawApply);
		return tbWithdrawApply;
	}
	@Transactional(readOnly = true)
	public Page<TbWithdrawApply> findDrawApplyPage(Page<TbWithdrawApply> page, TbWithdrawApply tbWithdrawApply) {

		// 设置默认时间范围，默认当前月
		/*if (tbWithdrawApply.getBeginDate() == null){
			tbWithdrawApply.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
		}
		if (tbWithdrawApply.getEndDate() == null){
			tbWithdrawApply.setEndDate(DateUtils.addMonths(tbWithdrawApply.getBeginDate(), 1));
		}*/
		Date date=tbWithdrawApply.getEndDate();
		if(date!=null){
			date.setHours(23);
			date.setMinutes(59);
			date.setSeconds(59);
		}
		return super.findPage(page, tbWithdrawApply);

	}
    @Transactional(readOnly = false)
	public void updateWithDrawalStatus(TbWithdrawApply tbWithdrawApply) {
        //// TODO: 11/26/2016 冻结金额－提款金额

        TbWithdrawApply tbWithdraw = tbWithdrawApplyMapper.selectByPrimaryKey(Integer.valueOf(tbWithdrawApply.getId()));
            TbMpWallet tbMpWallet = tbMpWalletMapper.getByUID(tbWithdraw.getUid());
        if("1".equals(tbWithdrawApply.getAuditorStatus())){//审核通过
			BigDecimal frozenMoney = tbMpWallet.getFrozenMoney().subtract(tbWithdraw.getMoney());
            tbMpWallet.setFrozenMoney(frozenMoney);
        }else if("2".equals(tbWithdrawApply.getAuditorStatus())){//审核不通过
			BigDecimal frozenMoney =tbMpWallet.getFrozenMoney().subtract(tbWithdraw.getMoney());
			BigDecimal totalMoney =tbMpWallet.getTotalMoney().add(tbWithdraw.getMoney());
			tbMpWallet.setFrozenMoney(frozenMoney);
            tbMpWallet.setTotalMoney(totalMoney);
        }
		tbWithdrawApplyMapper.updateWithDrawalStatus(tbWithdrawApply);
        tbMpWalletMapper.updateMqWallet(tbMpWallet);
	}
}
